package org.dy.util;

import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;

public class GlobalValue {

	private static final Locale currentLocale = Locale.CHINESE;

	private static final String RESOURCE_FILE_PRE = "resource";

	private static ResourceBundle rs = ResourceBundle.getBundle(
			RESOURCE_FILE_PRE, currentLocale);

	public static String getValue(String key) {
		String value = rs.getString(key);
		if (value == null)
			return key;
		else if (value.trim().equals(""))
			return key;
		return getUnicode(rs.getString(key));
	}

	public static String getValue(String key, Locale locale) {
		rs = ResourceBundle.getBundle(RESOURCE_FILE_PRE, locale);
		String value = rs.getString(key);
		if (value == null)
			return key;
		else if (value.trim().equals(""))
			return key;
		return getUnicode(rs.getString(key));
	}

	public static int getIntValue(String key) {
		String value = rs.getString(key);
		return Integer.parseInt(value);
	}

	// To Unicode
	private static String getUnicode(String sourceStr) {
		try {
			String encode = "ISO-8859-1";
			String temp_p = sourceStr;
			byte[] temp_t;
			temp_t = temp_p.getBytes(encode);
			String temp = new String(temp_t);

			return temp;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
